Back to course home
0% completed
Vote For New Content
Arithmetic Slices II - Subsequence (hard)
Problem Statement
Given an array of integers nums, return the number of arithmetic subsequences of nums.
If any integer array consists of at least three elements and if the difference between any two consecutive elements is the same, it is called arithmetic.
- For example
[3, 6, 9, 12]is arithmetic sequences.
A subsequence of an array is a sequence that can be formed by eliminating some or no elements from the array.
Examples
-
Example 1:
- Input: nums =
[1, 2, 3, 4] - Expected Output:
3 - Justification: The possible arithmetic subsequences are
[1, 2, 3],[2, 3, 4], and[1, 2, 3, 4]. Each has a common difference of 1. These are the only subsequences that meet the criteria.
- Input: nums =
-
Example 2:
- Input: nums =
[9, 4, 7, 2, 10] - Expected Output:
1 - Justification: The possible arithmetic subsequence is
[4, 7, 10]. It has a consistent difference between consecutive elements, which is 3.
- Input: nums =
-
Example 3:
- Input: nums =
[3, 6, 9, 12, 15] - Expected Output:
7 - Justification: The arithmetic subsequences are
[3, 6, 9],[6, 9, 12],[9, 12, 15],[3, 6, 9, 12],[6, 9, 12, 15],[3, 6, 9, 12, 15], and[3, 9, 15].
- Input: nums =
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Candy
Solution: Arithmetic Slices II - Subsequence
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself